﻿using System;
using System.Collections.Generic;
using System.Web;
using ARS.Control.Factory;
using ARS.DataBase.Provider;
using DbFactory = ARS.Control.Factory.DbFactory;

namespace ARS.Control.WebForms
{
    public class CheckControlConfig : IHttpHandler
    {
        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            //在此写入您的处理程序实现。

            string type = context.Request.QueryString["type"];
            if (type == null)
            {
                context.Response.Write("<a href='?type=datapaging&action=check'>分页控件</a><br/>");
                context.Response.Write("<a href='?type=datapaging&action=userinfo'>用户配置</a>");
            }
            else
            {
                IDbProvider dbProvider = ARS.DataBase.Provider.DbProvider.GetInstance();
                if (type == "datapaging")
                {
                    string action = context.Request.QueryString["action"];
                    if (action == "check")
                    {
                        const string tableName = "ARS_DataPaging";
                        if (UserFactory.CreateUser() == null)
                        {
                            context.Response.Write(
                                "用户配置不正确！<add KeyField=\"UserInfoAssembly\" value=\"程序集\" /><add KeyField=\"UserInfoClass\" value=\"命名空间.类名\" />");
                        }
                        //变是否存在
                        if (dbProvider.ExistsTable(tableName))
                        {
                            if (ARS.DataBase.Provider.DbFactory.DbTypeIsMySql())
                            {
                                Dictionary<string, string> columnList = new Dictionary<string, string>();
                                columnList.Add("DbOperat_OperatSqlList", "varchar(255) DEFAULT NULL");
                                columnList.Add("DbOperat_SearchList", "varchar(255) DEFAULT NULL");
                                //ArrayList<string, string> columnList = new ArrayList<string, string>();
                                //string[] tableList = new string[]
                                //{
                                //    "DbOperat_OperatSqlList"
                                //};
                                foreach (var item in columnList)
                                {
                                    if (!dbProvider.ExistsColumn(tableName, item.Key))
                                    {
                                        dbProvider.ExecuteSql(string.Format("alter table {0} add column {1} {2};", tableName, item.Key, item.Value));
                                    }
                                }
                            }
                        }
                        else
                        {
                            context.Response.Write("DataPaging表不存在!<a href='?type=datapaging&action=createtable'>创建表</a></br>");
                        }
                        context.Response.Write("<a href='?type=datapaging&action=createProc'>更新存储过程</a>");
                    }
                        //创建表
                    else if (action=="createtable")
                    {
                        string sql = null;
                        if (ARS.DataBase.Provider.DbFactory.DbTypeIsMySql())
                        {
                            sql =
                                "DROP TABLE IF EXISTS `ARS_DataPaging`;CREATE TABLE `ARS_DataPaging` (`Id` int(11) NOT NULL AUTO_INCREMENT, `Status_ClassName` varchar(255) DEFAULT NULL,`DbSelect_TableName` varchar(255) DEFAULT NULL,`DbSelect_SelectFields` varchar(255) DEFAULT NULL,`DbSelect_OrderField` varchar(255) DEFAULT NULL,`DbSelect_PageIndex` int(11) DEFAULT NULL, `DbSelect_PageSize` int(11) DEFAULT NULL,`DbSelect_StrWhere` varchar(255) DEFAULT NULL,`DbSelect_KeyField` varchar(255) DEFAULT NULL,`HtmlTable_TableCssName` varchar(255) DEFAULT NULL,`HtmlTable_TBodyTrHtml` varchar(4000) DEFAULT NULL,`Operat_OperatHtml` varchar(255) DEFAULT NULL,`Operat_IsHasOperat` bit(1) DEFAULT NULL,`Operat_ColumnName` varchar(255) DEFAULT NULL,`DbOperat_DeleteSql` varchar(255) DEFAULT NULL,`Status_UserId` int(11) DEFAULT NULL,`HtmlFanye_FanyeHtml` varchar(4000) DEFAULT NULL,`HtmlFanye_IsHasFanye` bit(1) DEFAULT NULL,`DbSelect_OrderType` bit(1) DEFAULT NULL,`HtmlTable_THeadTrHtml` varchar(255) DEFAULT NULL,`Where_KeyValue` varchar(255) DEFAULT NULL,`DbOperat_OperatSqlList` varchar(255) DEFAULT NULL,PRIMARY KEY (`Id`)) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;";
                        }
                        else if (ARS.DataBase.Provider.DbFactory.DbTypeIsSqlServer())
                        {
                            //sql = Res_SqlStr.sqlserver_datapagding_create;
                        }
                        dbProvider.ExecuteSql(sql);
                    }
                    //创建存储过程
                    else if (action == "createProc")
                    {
                        //ARS.PublicTool.FileUtility fileUtility = new PublicTool.FileUtility();
                        if (ARS.DataBase.Provider.DbFactory.DbTypeIsMySql())
                        {
                            dbProvider.ExecuteSql(Res_SqlStr.DataPagingProcMySql);
                        }
                        else if (ARS.DataBase.Provider.DbFactory.DbTypeIsSqlServer())
                        {

                        }
                        
                        context.Response.Write("更新成功!");
                    }

                    //
                    //if (action == "")
                    //{

                    //}
                }
            }

        }

        #endregion
    }
}
